package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Orders> implements OrderService {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    public void submit(Orders orders) {
        //1. 数据准备工作
        //1.1 查询当前用户购物车列表
        LambdaQueryWrapper<ShoppingCart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(cartLambdaQueryWrapper);

        //1.2 根据地址ID查询地址信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());

        //1.3 根据当前登录用户ID查询用户信息
        User user = userService.getById(BaseContext.getCurrentId());

        //2. 组装订单明细的数据 -------> List<OrderDetail>
        long orderId = IdWorker.getId();//orderId ------> 雪花算法生成
        BigDecimal totalAmount = new BigDecimal("0");

        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setName(cart.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setOrderId(orderId);

            //累加, 计算总金额
            //totalAmount += amount * number;
            totalAmount = totalAmount.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));

            orderDetailList.add(orderDetail);
        }


        //3. 组装订单的数据
        orders.setId(orderId);//订单表主键ID
        orders.setNumber(String.valueOf(orderId));//订单号
        orders.setUserId(BaseContext.getCurrentId());//当前用户ID
        orders.setAddress(addressBook.getDetail());//收件地址
        orders.setConsignee(addressBook.getConsignee());//收件人
        orders.setPhone(addressBook.getPhone());//收件人手机号
        orders.setStatus(2);//待派送
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setUserName(user.getName());
        orders.setAmount(totalAmount);//总金额

        //4. 保存订单明细, 订单
        this.save(orders);
        orderDetailService.saveBatch(orderDetailList);

        //5. 删除当前用户购物车数据
        shoppingCartService.remove(cartLambdaQueryWrapper);
    }
}
